package com.users.rn.rncommon.basic;

import androidx.annotation.NonNull;

import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.uimanager.ViewManager;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * 作者：wolf on 12/22/20 16:20
 * <p>
 * 邮箱：bluce
 */

public class RNReactModulesPackage implements ReactPackage {
    private final static HashSet<Class<? extends NativeModule>> extendModuleClass = new HashSet<>();
    private final static HashSet<Class<? extends ViewManager>> extendViewClass = new HashSet<>();

    /**
     * 添加自定义的NativeModule
     *
     * @param clazzes
     */
    public static void registerNativeModule(Class<? extends NativeModule>... clazzes) {
        if (clazzes != null) {
            for (Class<? extends NativeModule> clazz : clazzes) {
                extendModuleClass.add(clazz);
            }
        }
    }

    /**
     * 添加自定义的View
     *
     * @param clazzes
     */
    public static void registerViewManager(Class<? extends ViewManager>... clazzes) {
        if (clazzes != null) {
            for (Class<? extends ViewManager> clazz : clazzes) {
                extendViewClass.add(clazz);
            }
        }
    }

    @NonNull
    @Override
    public List<NativeModule> createNativeModules(@NonNull ReactApplicationContext reactContext) {
        List<NativeModule> list = new ArrayList<>();
        for (Class<? extends NativeModule> clazz : extendModuleClass) {
            try {
                NativeModule nativeModule = null;
                if (ReactContextBaseJavaModule.class.isAssignableFrom(clazz)) {
                    nativeModule = clazz.getConstructor(ReactApplicationContext.class).newInstance(reactContext);
                } else {
                    nativeModule = clazz.getConstructor().newInstance();
                }
                if (nativeModule != null) {
                    list.add(nativeModule);
                }
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    @NonNull
    @Override
    public List<ViewManager> createViewManagers(@NonNull ReactApplicationContext reactContext) {
        List<ViewManager> list = new ArrayList<>();
        for (Class<? extends ViewManager> clazz : extendViewClass) {
            try {
                ViewManager nativeModule = clazz.newInstance();
                if (nativeModule != null) {
                    list.add(nativeModule);
                }
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return list;
    }
}
